package babo.cd.physicalexperiment.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.TwoLevelHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import babo.cd.physicalexperiment.BaseActivity;
import babo.cd.physicalexperiment.R;
import babo.cd.physicalexperiment.bean.NoticeListBean;
import babo.cd.physicalexperiment.databinding.ActivityShebeilistBinding;
import babo.cd.physicalexperiment.utils.OKCallback;
import babo.cd.physicalexperiment.utils.OkUtils;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;

public class NoticeListActivity extends BaseActivity {
    ActivityShebeilistBinding binding;
    private int pi=1;
    List<NoticeListBean.NoticeInfo> datas=new ArrayList<>();
    BaseQuickAdapter adapter;

    @Override
    protected View bindView() {
        binding=ActivityShebeilistBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        binding.topbar.setCenterText("通知列表");
        binding.smr.setRefreshHeader(new TwoLevelHeader(this));
        binding.smr.setRefreshFooter(new ClassicsFooter(this));
        binding.smr.setEnableLoadMore(true);
        binding.rcv.setLayoutManager(new LinearLayoutManager(context));
        adapter=new BaseQuickAdapter(R.layout.item_mynotices,datas) {
            @Override
            protected void convert(BaseViewHolder helper, Object item) {
               bindConvert(helper,(NoticeListBean.NoticeInfo)item);
            }
        };
        binding.rcv.setAdapter(adapter);
    }

    private void bindConvert(BaseViewHolder helper, NoticeListBean.NoticeInfo item) {
        TextView txtime=helper.getView(R.id.txtime);
        TextView txcontent=helper.getView(R.id.txcontent);
        ImageView ivnew=helper.getView(R.id.ivnew);
        txtime.setText(item.getCreateTime());
        txcontent.setText(item.getContent());
        if (item.getIsRead()==0){
          ivnew.setVisibility(View.VISIBLE);
          doHaveRead(item.getId());
        }else{
            ivnew.setVisibility(View.GONE);
        }

    }

    private void doHaveRead(String id) {
        OkUtils.GetOk(URLS.setMessageRead+"?messageId="+id,new OKCallback(context){
            @Override
            public void Sucess(String str) {
                super.Sucess(str);



            }

        });
    }

    @Override
    protected void loadData() {
     getNotice();
    }

    private void getNotice() {

        OkUtils.GetOk(URLS.getMessageList+"?pi="+pi+"&ps=10",new OKCallback(context){
            @Override
            public void Sucess(String str) {
                Out.out("返回数据："+str);
                super.Sucess(str);

                try {
                    binding.smr.finishRefresh(300);
                }catch (Exception ex){}
                try {
                    binding.smr.finishLoadMore(300);
                }catch (Exception ex){}

                try {

                    NoticeListBean noticeListBean=gson.fromJson(str,NoticeListBean.class);
                    if (noticeListBean.getData().getRecords().size()>0){

                        binding.txNodata.setVisibility(View.GONE);
                        datas.addAll(noticeListBean.getData().getRecords());
                        adapter.notifyDataSetChanged();


                    }else{
                        if (pi==1){
                            binding.txNodata.setVisibility(View.VISIBLE);

                        }else{

                            Out.toast(context,"已加载完所有数据~");

                        }
                    }
                }catch (Exception ex){
                    ex.printStackTrace();

                    if (pi==1){
                        binding.txNodata.setVisibility(View.VISIBLE);
                    }else{
                        Out.toast(context,"已加载完所有数据~");
                    }
                }

            }

            @Override
            public void Eorror(int code) {
                super.Eorror(code);
                Out.out("通知code="+code);
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void setListener() {
        super.setListener();
        binding.smr.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
             pi++;
             getNotice();
            }
        });
        binding.smr.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
             pi=1;
             datas.clear();
             getNotice();
            }
        });

    }
}
